Ext.define('Portal.controller.AppController',{
    extend:'Ext.app.Controller',
    alias:'controller.appController',
    listen : {
        global: {
            postMessage:'postMessage',
            importExternalFile:'importExternalFile',
            getApi:'getApi',
            postApi:'postApi'
        }
    },
    postMessage:function (obj,callback) {
        var iframe = document.getElementById(obj.id);
        if(!iframe) return;
        iframe.contentWindow.postMessage(obj.message, obj.targetOrigin);
        function receiveMessage(event){
            if (event.origin !== obj.targetOrigin) return;
            callback(event)
        }
        window.addEventListener("message", receiveMessage, false);
    },
    importExternalFile:function (obj) {
        this.removeScript(obj);
        var hook = Hooks();hook.initEnv();
        function hookLoader(classNames) {
            for(var k=0;k<classNames.length;k++){
                if(classNames[k].indexOf(obj.controllerName)>=0){
                    classNames[k] = classNames[k]+'1'
                }
            }
        }
        function hookLoadScripts(params){
            for(var i=0;i<params._classNames.length;i++){
                if(params._classNames[i].indexOf(obj.controllerName) >=0){
                    params._urlByClass[obj.controllerName] =params._urlByClass[obj.controllerName+1].replace(/1/,'');
                    delete params._urlByClass[obj.controllerName+1];
                    params._classNames[i] = obj.controllerName;
                    params.url[i] = obj.controllerUrl;
                }
            }
            console.log(params);
            return params
        }
        Ext.Loader.load.hook(Ext.Loader.load,hookLoader,Ext.Loader,'load');
        Ext.Loader.loadScripts.hook(Ext.Loader.loadScripts,hookLoadScripts,Ext.Loader,'loadScripts');

        Ext.require([
            obj.controllerName
        ]);
    },
    removeScript:function (obj) {
        var scripts = document.getElementsByTagName('script');
        for(var n=0;n<scripts.length;n++){
            if(scripts[n].src.indexOf(obj.controllerUrl)>=0){
                Ext.undefine(obj.controllerName+1);
                scripts[n].remove();

            }
        }
    },
    createRequest:function (method,path,data,callback) {
        var baseUrl = Portal.getApplication().config.path;
        var self = this;
        if(typeof callback === 'function') {
            callback = {
                success: callback
            };
        }
        var ajaxOptions ={
            url: baseUrl+path ,
            method:method,
            params:data,

            timeout:20000,
            success:function(response, opts) {
                var data = JSON.parse(response.responseText);
                if(data.success) {
                    callback.success.call(self,data.data);
                } else {
                    console.log('server-side failure with status code ' + data.msg);
                }
            },
            failure:function(response, opts) {
                console.log('server-side failure with status code ' + response.status);
            }
        };
        return Ext.Ajax.request(ajaxOptions);
    },
    getApi:function (path) {
        var params = [];
        if(arguments.length==2) {
            params = [path,{},arguments[1]];
        } else {
            params = [path,arguments[1],arguments[2]];
        }
        params.unshift('GET');
        return this.createRequest.apply(this,params);
    },
    postApi:function(path,data,callback) {
        return this.createRequest('POST',path,data,callback);
    },
});